<?php
namespace Admin\Controller;

class NoticeController extends BaseController {

    public function index() {

        $Notice = D ('Notice');
        $map['teacher_id'] = $this->admin['id'];
        $map['term_id'] = $this->term['id'];

        // 分页处理
        $limit = \Api\Libs\PageUtil::getLimit(10);
        $page = \Api\Libs\PageUtil::getPageHtml($Notice->getCountByWhere($map),10);
        $noticeList = $Notice->getNoticeByWhere($map,$limit);

        $this->assign('noticeList',$noticeList);
        $this->assign('page',$page);
        $this->display();
    }

    public function send() {

        if (!IS_POST) {

            // 获取该老师的教学计划和能管理的班级信息
            $teacher_id = $this->admin['id'];

            // 获取当前教师能管理的所有班级
            $ClassManager = D ('ClassManager');
            $classIds = $ClassManager->getClassIdsByTid($teacher_id);
            $classList = array();
            if (!is_null($classIds)) {
                $Class = D ('Class');
                $classList = $Class->getClassArrByIdArr($classIds);
            }

            if (!empty($classList)) {
                foreach ($classList as $k1=>$v1) {
                    $classList[$k1]['type'] = 'class';
                }
            }

            // 获取当前教师所有的教学计划
            $Teachplan = D ('Teachplan');
            $map['teacher_id'] = $teacher_id;
            $map['term_id'] = $this->term['id'];
            $teachplanList = $Teachplan->getTeachplanByWhere($map);

            if (!empty($teachplanList)) {
                foreach ($teachplanList as $k2=>$v2) {
                    $teachplanList[$k2]['type'] = 'teachplan';
                }
            }

            $groupList = array_merge($classList,$teachplanList);
            $this->assign('groupList',$groupList);

            unset($classList);
            unset($teachplanList);
            unset($groupList);
            unset($ClassManager);
            unset($Class);
            unset($Teachplan);
            unset($map);

            $this->display();

        } else { // 提交请求

            $data['term_id'] = $this->term['id'];
            $data['teacher_id'] = $this->admin['id'];
            $data['title'] = I ('request.title','');
            $data['content'] = I ('request.content','');

            // 此处应该限制一下内容长度

            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

            // 更具groupid 获取学生信息
            $Student = D ('Student');
            $Teachplan = D ('Teachplan');
            $group_ids = I ('request.group_id','');


            // 循环获取 student id
            $newIdsArr = array();
            if (!empty($group_ids)) {
                foreach ($group_ids as $v) {
                    list ($id,$type) = explode('-',$v);

                    switch (strtoupper($type)) {
                        case "CLASS":
                            $newIdsArr = array_merge($Student->getSidsByCid($id),$newIdsArr);
                            break;
                        case 'TEACHPLAN':
                            $newIdsArr = array_merge($Teachplan->getSidsArrByTid($id),$newIdsArr);
                            break;
                        default:
                    }
                }
            }

            $student_ids = I ('request.student_id','');
            !empty($student_ids) && $newIdsArr = array_merge($newIdsArr,$student_ids);

            // 去除数组中得重复数据
            $newIdsArr = array_unique($newIdsArr);
            // 生成学生id字符串
            $data['student_ids'] = implode(',',$newIdsArr);
            if (empty($data['student_ids'])) $this->out('RESULT_FAIL','请添加上课学生');


            // 极光推送接口，每次只能定位1000个用户
            // 统计使用极光需要分多少次发送
            $count = count($newIdsArr);
            $times = intval(ceil($count / 1000));
            $idGroup = array();
            for ($i = 1; $i <= $times; $i++) {
                $idGroup[] = array_slice($newIdsArr,($i-1)*1000,1000);
            }

            $JPush = new \Api\Libs\JPush();
            $msg = array(
                'title' => $data['title'],
                'content' => $data['content'],
            );
            // 客户端接收参数
            $extras = array('action' => 1);

            // 调用接口发送
            $JpushMapping = D ('JpushMapping');
            $successCount = 0;
            foreach ($idGroup as $k=>$v) {
                $receiver = $JpushMapping->getReceiver($v);
                $result = json_decode($JPush->pushNotification($msg,$receiver,$extras),true);
                // 发送成功
                if (empty($result['error'])) {
                    $successCount ++ ;
                }
            }

            // 将推送信息存储到数据库
            if ($successCount !== 0) {
                $data['status'] = 1;
            }
            $data['addtime'] = date('Y-m-d H:i:s');

            $flag = D ('Notice')->_add($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','推送成功',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }

    }



    public function getStudent() {
        if (!IS_POST) $this->out('RESULT_FAIL','非法访问');

        $value = I ('request.value');
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($value)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));

        list($id,$type) = explode('-',$value);
        switch (strtoupper($type)) {
            case "CLASS":
                $studentList = D ('Student')->getStudentByCid($id);
                break;
            case "TEACHPLAN":
                $studentIdStr = D ('Teachplan')->where('id = '.$id)->getField('student_ids');
                $studentIdArr = explode(',',$studentIdStr);
                $studentList = D ('Student')->getStudentArrByIdArr($studentIdArr);
                break;
            default:
                $this->out('RESULT_FAIL','错误的类型');
        }

        //$map['class_id'] = $classid;
        //is_null($this->selectedSids) && $map['id'] = array('not in',$this->selectedSids);
        //$studentList = D ('Student')->getStudentByCid($classid);
        $this->out('RESULT_OK','',$studentList);
    }

}
